<?php

namespace App\Models\Order;

use App\Models\Base;
class Order extends Base
{
    /**
     * 关联到模型的数据表
     *
     * @var string
     */
    protected $table = 'order';


    public function saveOrder($data){
        $res = $this->insertGetId($data);
        return $res;
    }

    public function order_goods(){
        return $this->hasMany('App\Models\Order\OrderGoods', 'order_id', 'order_id');
    }

    public static function getOrderById($id){
        $res = self::with('order_goods')->find($id)->toArray();
        if ($res){
            $arr = $res['order_goods'];
            $res['num'] = array_sum(array_column($arr,'num'));
            return $res;
        }
        return false;
    }

}
